<template>
  <!-- 设备型号 -->
  <div class="filterContent">
    <a-form :model="filterForm" layout="inline">
      <a-form-item label="厂商简称">
        <a-select
          v-model:value="filterForm.supplierId"
          class="formInput"
          placeholder="请选择"
          @change="changeSupplierType"
          @clear="handleClear"
          allow-clear
        >
          <a-select-option
            v-for="item in supplierList"
            :value="item.supplierId"
            :key="item.supplierId"
          >
            {{ item.supplierShortName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="协议型号">
        <a-select
          v-model:value="filterForm.supplierTypeId"
          class="formInput"
          placeholder="请选择"
          :disabled="!filterForm.supplierId"
          allow-clear
        >
          <a-select-option
            v-for="item in supplierTypeList"
            :value="item.id"
            :key="item.id"
          >
            {{ item.type }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="设备型号">
        <a-input
          v-model:value="filterForm.name"
          class="formInput"
          placeholder="请输入"
          allow-clear
        />
      </a-form-item>
      <a-button type="primary" class="filterBtn" @click="handleFilter"
        >查询</a-button
      >
      <a-button type="primary" class="filterBtn greenBtn" @click="handleModel"
        >新增</a-button
      >
    </a-form>
  </div>
  <div class="tableContent">
    <a-table
      :dataSource="tableData"
      :columns="columns"
      size="small"
      :scroll="{ x: 'max-content' }"
      @resizeColumn="handleResizeColumn"
      :pagination="false"
      :loading="tableLoading"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'businessType'">
          {{ BUSINESS_TYPE[record.businessType] }}
        </template>
        <template v-if="column.key === 'handle'">
          <a-button type="link" @click="handleEdit(record)">编辑</a-button>
          <a-popconfirm
            ok-text="确认"
            cancel-text="取消"
            @confirm="confirmDel(record)"
          >
            <template #title>
              是否确定删除【{{ record.supplierShowValue }}】【{{ record.name }}】的记录?
            </template>
            <a-button type="link" danger>删除</a-button>
          </a-popconfirm>
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="filterForm.current"
      v-model:page-size="filterForm.size"
      :total="total"
      position="bottomRight"
      show-size-changer
      @change="getEquipModelPage"
      :show-total="(total) => `总共 ${total} 条`"
    >
      <template #buildOptionText="props">
        <span>{{ props.value }}条/页</span>
      </template>
    </a-pagination>
  </div>
  <!-- 新增型号 -->
  <AddModel
    :addModelVisible="addModelVisible"
    :rowData="rowData"
    :isEdit="isEdit"
    @update:addModelVisible="(val) => (addModelVisible = val)"
    @getEquipModelPage="handleFilter"
  />
</template>
<script setup>

import AddModel from "./conpoments/addModel.vue";

const $api = inject("$api");
const filterForm = ref({
  current: 1,
  size: 10,
  name: null,
  supplierTypeId: null,
  supplierId: null,
});
const labelCol = { style: { width: "100px" } };
const total = ref();
const addModelVisible = ref(false);
const tableData = ref([]);
const tableLoading = ref(false);
const rowData = ref({});
const isEdit = ref(false);
const columns = ref([
  {
    title: "厂商简称",
    dataIndex: "supplierShowValue",
    key: "supplierShowValue",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "设备型号",
    dataIndex: "name",
    key: "name",
    width: 150,
    resizable: true,
    align: "center",
  },
  {
    title: "协议型号",
    dataIndex: "typeShowValue",
    key: "typeShowValue",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "设备生产厂家",
    dataIndex: "equipmentManufacturerDesc",
    key: "equipmentManufacturerDesc",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "单柜容量(kWh)",
    dataIndex: "singleCapacity",
    key: "singleCapacity",
    width: 140,
    resizable: true,
    align: "center",
  },
  {
    title: "单柜额定功率(kW)",
    dataIndex: "singleRatedPower",
    key: "singleRatedPower",
    width: 160,
    resizable: true,
    align: "center",
  },
  {
    title: "PACK数量",
    dataIndex: "packNumberPerCluster",
    key: "packNumberPerCluster",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "电芯数量",
    dataIndex: "cellNumberPerPack",
    key: "cellNumberPerPack",
    width: 150,
    resizable: true,
    align: "center",
  },
  {
    title: "创建人",
    dataIndex: "createUserName",
    key: "createUserName",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    width: 140,
    resizable: true,
    align: "center",
  },
  {
    title: "更新人",
    dataIndex: "updateUserName",
    key: "updateUserName",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    width: 140,
    resizable: true,
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "handle",
    key: "handle",
    width: 120,
    resizable: true,
    align: "center",
    fixed: "right",
  },
]);
function handleResizeColumn(w, col) {
  col.width = w;
}
const handleModel = () => {
  isEdit.value = false;
  addModelVisible.value = true;
};
const handleEdit = async (data) => {
  rowData.value = data;
  isEdit.value = true;
  addModelVisible.value = true;
};
const getEquipModelPage = async () => {
  const params = {
    ...filterForm.value,
  };
  tableLoading.value = true;
  const res = await $api.equipModelPage(params);
  tableLoading.value = false;
  if (res && res.code === 0) {
    tableData.value = res.data.records;
    total.value = res.data.total;
  }
};
const confirmDel = async (data) => {
  const res = await $api.deleteEquipModelPage(data.id);
  if (res && res.code === 0) {
    message.success("删除成功");
    getEquipModelPage();
  }
};
const handleClear = () => {
  filterForm.value.supplierTypeId = null;
  filterForm.value.supplierId = null;
};
const supplierList = ref([]);
const supplierTypeList = ref([]);
const getSupplierTypeTree = async () => {
  const res = await $api.supplierTypeTree();
  supplierList.value = res.data;
};
const changeSupplierType = (id) => {
  filterForm.value.supplierTypeId = null;
  supplierTypeList.value =
    supplierList.value.filter((item) => item.supplierId === id)[0]
      ?.supplierTypeEntities || [];
};
const handleFilter = () => {
  filterForm.value.current = 1;
  filterForm.value.size = 10;
  getEquipModelPage();
};
onMounted(async () => {
  getEquipModelPage();
  getSupplierTypeTree();
});
</script>

<style lang="less" scoped>
.ant-table {
  .ant-btn {
    padding: 4px 5px;
  }
}
.formInput {
  width: 155px;
}
.filterBtn {
  margin: 0 20px 20px 0;
  &:last-child {
    margin-right: 0;
  }
}
.tableContent {
  border-radius: 4px;
  background-color: #141414;
  padding: 8px;
}
</style>
